<?php
require_once 'models/battle_items.php';
require_once 'dbhelper.php';

class battles_item_controller extends dbhelper {
    public function getAllBattles_item($id_battle)
    {
        $battles = array();
        $sql = "SELECT * FROM battle_items";
         $sql .=" where battle_id = $id_battle";
        
        $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $battle_item = new battle_items($row["id"],$row["battle_id"],$row["title"],$row["file_name"],$row["fb_theme"],$row["yt_link"],$row["votes"],$row['last_votes']);
                $battles[count($battles)] = $battle_item;
            }
            //var_dump($battles);exit();
            //$_SESSION['search'] = '';
        return $battles;
    }
    public function getslideBattles_item($id_battle)
    {
        $battles = array();
        $sql = "SELECT * FROM battle_items";
         $sql .=" where battle_id = $id_battle LIMIT 0,2";
        
        $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $battle_item = new battle_items($row["id"],$row["battle_id"],$row["title"],$row["file_name"],$row["fb_theme"],$row["yt_link"],$row["votes"],$row['last_votes']);
                $battles[count($battles)] = $battle_item;
            }
            //var_dump($battles);exit();
            //$_SESSION['search'] = '';
        return $battles;
    }
    public function getBattlesById($id)
    {
        $id = $id;
        $sql = "SELECT * from battles where id = '$id'";
        $result = parent::execQuery($sql);
        if($result && parent::getNumRows($result))
        {
            $row = parent::fetchArray($result);
            $battle = new users($row["id"],$row["title"],$row["sub_title"],$row["type"],$row["running"],$row["max_vote"],$row["vote_start_from"],$row["date_created"],$row["last_update"]);
        }
        return $battle;
    }
    
    
    public function addBattles_item($battle_item) {
        $sql = "INSERT INTO battle_items(battle_id,title,file_name,fb_theme,yt_link,votes,last_votes) VALUES('$battle_item->battle_id', '$battle_item->title', '$battle_item->file_name', '$battle_item->fb_theme', '$battle_item->yt_link', '$battle_item->votes', '$battle_item->last_votes')";
        parent::execQuery($sql);
        //var_dump($sql);exit;
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
    public function deleteBattlesbyId($id)
    {
        $sql = "DELETE FROM battle_items WHERE id = '$id'";
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
    public function upload_file_img($file)
    {
        //echo $_FILES["$file"]["type"];exit;
        if(($_FILES["$file"]["type"] == "image/jpeg")||($_FILES["$file"]["type"] == "image/gif") && ($_FILES["$file"]["size"] < 200000))
	{
            $name_p3 = $_GET['id']. '_' .$_GET['id_bt'] . '_' . $_FILES["$file"]["name"];
		if($_FILES["$file"]["error"] >0)
		{
			echo"file bi loi ";
		}else{
			move_uploaded_file($_FILES["$file"]["tmp_name"],'../file/'. $name_p3);
		}		
		$path_th="file/". $name_p3;
	}
        return $path_th;
    }
    public function upload_file_video($file)
    {
        //var_dump($_FILES["video_f"]["size"]);
        //var_dump($_FILES["video_f"]["tmp_name"]);exit;
        
	{
                //var_dump($_FILES["video_f"]["size"]);
                //var_dump($_FILES["video_f"]["size"]);
                //var_dump($_FILES["video_f"]["tmp_name"]);exit;
            $fileName = $_FILES["$file"]["name"];
            $fileNameParts = explode( ".", $fileName );
            $fileExtension = end( $fileNameParts );
            $fileExtension = strtolower( $fileExtension );
            $name_pv = $_GET['id']. '_' .$_GET['id_bt'] . '_' . $_FILES["$file"]["name"];
            if($fileExtension=="avi" || $fileExtension=="wmv" || $fileExtension=="mpeg" || $fileExtension=="mpg" || $fileExtension=="mov" || $fileExtension=="avi" || $fileExtension=="FLV" || $fileExtension=="flv" || $fileExtension=="mp4")
            {
                //var_dump($_FILES["video_f"]["size"]);
                //var_dump($_FILES["video_f"]["tmp_name"]);exit;
		if($_FILES["$file"]["error"] >0)
		{
                    echo"file bi loi";
		}else{
                    move_uploaded_file($_FILES["$file"]["tmp_name"],'../file/'. $name_pv);
		}
		$path="file/". $name_pv;
            }
        }
        return $path;
    }
    function totalVotes($battleID)
    {
	$sql = "select sum(votes) as TotalVotes from battle_items where battle_id='".$battleID."'";
	$rs = mysql_query($sql) or die("Cannot Execute Query : ".$sql."<P>".mysql_error());
	$row = mysql_fetch_array($rs);
	return $row['TotalVotes'];
    }
}
?>
